package com.bingxue.edu.common.job;

import com.bingxue.edu.management.shop.model.entity.Order;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.framework.job.DynamicJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

@Slf4j
@DisallowConcurrentExecution
@RequiredArgsConstructor
@DynamicJob(name="超时订单取消任务", cron = "0 */1 * * * ? ", paramClass = DemoJobParam.class)
public class OrderTimeOutCancelJob extends QuartzJobBean {

    private final OrderService orderService;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        List<Order> orderList = orderService.getTimeoutPendingPayOrders();
        //取消订单
        for (Order order:orderList) {
            try {
                orderService.cancelOrder(order.getId());
            } catch (Exception e) {
                log.error("取消订单失败", e);
            }
        }
    }
}
